#!/bin/bash
clear
if [[ "$EUID" -ne 0 ]]; then
  echo -e "\033[1;31m Anda tiada kebenaran untuk menjalankan skrip ini! \033[0m"
  exit 1
fi

TEXT='\033[30;107;2m'
GREEN='\033[92m'
YELLOW='\033[93m'
ERROR='\033[97;41m'
RESET='\033[0m'

until [[ $UserName =~ ^[a-zA-Z0-9_]+$ && $checkUser == 0 ]]; do
  read -p "Masukkan nama pengguna: " UserName
  checkUser=$(grep -w $UserName /usr/local/etc/v2ray/clients/.accounts | wc -l)
  if [[ $checkUser -ne 0 ]]; then
    echo -e "${ERROR} RALAT ${RESET}${TEXT} Nama pengguna sudah wujud! ${RESET}"
    exit 1
  fi
done
read -p "Masukkan tempoh aktif [Hari]: " DurationTime

clientUUID=$(cat /usr/local/etc/v2ray/clients/.accounts | grep -w $UserName | awk '{print $3}')
oldExpDate=$(cat /usr/local/etc/v2ray/clients/.accounts | grep -w $UserName | awk '{print $4}')
activeTime=$((($(date -d "${oldExpDate}" +%s) - $(date +%s)) / (86400)))
totalTime=$(expr $activeTime + $DurationTime + 1)
newExpDate=$(date -d +${totalTime}days +%F)
expireDate=$(date -d "${newExpDate}" +%F)
clientProto="vless"

sed -i "s|$UserNAme|d" /usr/local/etc/v2ray/clients/.accounts
echo -e "$user\t$uuid\t$exp_new" >>/usr/local/etc/v2ray/clients/.accounts
echo -e "${UserName}\t${clientProto}\t${clientUUID}\t${expireDate}" >>/usr/local/etc/v2ray/clients/.accounts

clear
echo
echo -e "${TEXT} ========================================================== ${RESET}"
echo -e "${TEXT} VLESS - BUAT AKAUN                                         ${RESET}"
echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
echo
echo -e "${YELLOW} Nama pengguna:${RESET} ${GREEN}$UserName${RESET}"
echo -e "${YELLOW} Tarikh luput :${RESET} ${GREEN}$expireDate${RESET}"
echo
echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
echo -e "${TEXT} ========================================================== ${RESET}"
echo
